package cn.exrick.xboot.config.wechat;

import cn.binarywang.wx.miniapp.api.WxMaMsgService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.api.impl.WxMaMsgServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Created by kellen on 2020/5/3. 微信小程序配置
 */
@Configuration
public class WxConfiguration {

    /**  微信小程序相关配置  */
    @Value("${wechat.mp.appId}")
    private String appId;

    @Value("${wechat.mp.secret}")
    private String appSecret;

    /**  微信支付相关配置  */
    @Value("${wechat.pay.mch-id}")
    private String payMchId;

    @Value("${wechat.pay.notify-url}")
    private String payNotifyUrl;

    @Value("${wechat.pay.api-V3-key}")
    private String payApiV3Key;

    @Value("${wechat.pay.private-key-path}")
    private String payPrivateKeyPath;

    @Value("${wechat.pay.private-cert-path}")
    private String payPrivateCertPath;

    /** 小程序的service 开始*/
    @Bean
    public WxMaService wxMaService() {
        WxMaService wxMaService = new WxMaServiceImpl();
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appId);
        config.setSecret(appSecret);
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }

    @Bean
    public WxMaMsgService wxMaMsgService() {
        WxMaMsgService wxMaMsgService = new WxMaMsgServiceImpl(wxMaService());
        return wxMaMsgService;
    }

    @Bean
    public WxMaUserService waUserService() {
        WxMaUserService WxMaUserService = new WxMaUserServiceImpl(wxMaService());
        return WxMaUserService;
    }

    @Bean
    public WxPayConfig wxMaPayConfig() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(payMchId);
        payConfig.setNotifyUrl(payNotifyUrl);
        payConfig.setTradeType("JSAPI");
        payConfig.setApiV3Key(payApiV3Key);
        payConfig.setPrivateKeyPath(payPrivateKeyPath);
        payConfig.setPrivateCertPath(payPrivateCertPath);
        return payConfig;
    }

    @Bean
    @Primary
    public WxPayService wxMaPayService(WxPayConfig wxMaPayConfig) {
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxMaPayConfig);
        return wxPayService;
    }
    /** 小程序的service 结束*/

}
